<?php

class Logs{

	/**
	 * 添加
	 *
	 * @param unknown_type $name
	 * @param unknown_type $parent
	 * @param unknown_type $ord
	 */
	function add($log_code,$content){
		
		$data['log_code'] 	= $log_code;
		$data['log_content'] 	= $content;
		$data['created'] 	= full_date();
		$data['ip'] 		= real_ip();
		$data['admin_id'] 	= admin_id();

		$m = M('Logs');
		//返回自增id
		$id = $m->add($data);
		
		return $id;
	}
	
	/**
	 * 修改
	 *
	 * @param unknown_type $id
	 * @param unknown_type $upItems
	 * @return unknown
	 */
	function update($id,$upItems){
		$m = M('Logs');
		return $m -> where('log_id='.$id)->save($upItems);
	}
	
	
	function lists($where = null,$page,$pageSize,&$allCount){
		
		$whereSql = ' 1=1 ';
		if(!is_null($where)){
			//查询条件
			$whereSql .= " AND (t.created BETWEEN '{$where['begin_time']}' AND '{$where['end_time']}') ";

			if($where['admin_id'] > 0){
				$whereSql .= " AND t.admin_id = {$where['admin_id']} ";
			}
		}
		
		
		//总计数
		$allCount = $this->lists_count($whereSql);
		
		//开始行
		$startIndex = ($page - 1) * $pageSize;
		
		$m = M('Logs');
		return $m->table('logs t')
		->join('admin a ON a.id = t.admin_id')
		->join('roles r ON r.role_value = t.log_code')
		->field("
				t.*,
				r.role_name AS log_code_name,
				a.real_name AS admin_name")
		->where($whereSql)
		->limit($startIndex. ',' . $pageSize)
		->order('t.log_id DESC')
		->select();
	}
	
	function lists_count($whereSql){

		$m = M('Logs');
		return $m->table('logs t')
		->join('admin a ON a.id = t.admin_id')
		->where($whereSql)
		->getField('COUNT(*)');
	}
	
	/**
	 * 
	 * 任务单列表
	 * @param $id
	 */
	function select_by_admin_id($id){
		$m = M('Logs');
		return $m ->table('logs t')
		->join('admin a ON a.id = t.admin_id')
		->where('t.admin_id='.$id)
		->field("
			t.*,a.real_name")
		->order('t.log_id ASC')
		->select();
	}
	
	function get_by_log_id($id){
		$m = M('Logs');
		return $m -> getBylogId($id);
	}
}